
package com.be.beadmin.device.impl;

import com.be.beadmin.device.entity.ToolInfo;
import com.be.beadmin.device.entity.ToolInfo_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.ToolInfoMapperEx;
import com.be.beadmin.device.repository.ToolInfoRepository;
import com.be.beadmin.device.service.ToolInfoService;
import com.be.beadmin.device.dto.input.ToolInfoDto;
import com.be.beadmin.device.dto.output.ToolInfoEx;
import com.be.beadmin.device.dto.input.ToolInfoQueryCriteria;
import com.be.beadmin.device.mapper.ToolInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author sly
* @date 2023-08-26
**/
@Service
@RequiredArgsConstructor
public class ToolInfoServiceImpl implements ToolInfoService {

    @Autowired
    private  ToolInfoRepository toolInfoRepository;
    @Autowired
    private  ToolInfoMapper toolInfoMapper;
    @Autowired
    private  ToolInfoMapperEx toolInfoExMapper;

    @Override
    public Page<ToolInfoEx> getToolInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ToolInfo> page = toolInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ToolInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ToolInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ToolInfoEx> page1 = page.map(toolInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public void addToolInfo(ToolInfoDto toolInfoDto) {
            ToolInfo result = toolInfoRepository.findToolInfoByIdAndDelete(toolInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("ToolInfo不存在,请重新查询");
            }
            String toolId = toolInfoDto.getToolId();
            if(toolId.equals("")){
                throw new BeadminException("工具编号不能为空");
            }
            ToolInfo one = toolInfoRepository.findToolInfoByToolIdAndDelete(toolId , 0);
            if(ObjectUtil.isNotEmpty(one)){
                throw new BeadminException("工具编码已存在,请重新查询");
            }
            ToolInfo toolInfo = toolInfoMapper.toEntity(toolInfoDto);
            toolInfoRepository.save(toolInfo);
    }

    @Override
    public void updateToolInfo(ToolInfoDto toolInfoDto) {
            ToolInfo toolInfo = toolInfoRepository.findToolInfoById(toolInfoDto.getId());
            toolInfo = toolInfoMapper.toEntity(toolInfoDto);
            toolInfoRepository.save(toolInfo);
    }


    @Override
    public void delToolInfo(String id) {
        ToolInfo toolInfo = toolInfoRepository.findToolInfoById(id);
        toolInfo.setDelete(1);
        toolInfoRepository.save(toolInfo);
    }

    @Override
    public void delToolInfos (List<String> ids) { }

    //根据领用日期筛选
    @Override
    public ToolInfo getToolInfoByTakeawayDate(Date takeawayDate) {
        ToolInfo toolInfo = toolInfoRepository.findToolInfoByTakeawayDateAndDelete(takeawayDate, 0);
        if (toolInfo == null) {
            throw new BeadminException("未找到该项目");
        }
        return toolInfo;
    }

    //根据报废日期筛选
    @Override
    public ToolInfo getToolInfoByScrappedDate(Date scrappedDate) {
        ToolInfo toolInfo = toolInfoRepository.findToolInfoByScrappedDateAndDelete(scrappedDate, 0);
        if (toolInfo == null) {
            throw new BeadminException("未找到该项目");
        }
        return toolInfo;
    }

    //根据采购日期筛选
    @Override
    public ToolInfo getToolInfoByContractDate(Date contractDate) {
        ToolInfo toolInfo = toolInfoRepository.findToolInfoByContractDateAndDelete(contractDate, 0);
        if (toolInfo == null) {
            throw new BeadminException("未找到该项目");
        }
        return toolInfo;
    }
}